// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   GatherExpiry.java

package include.auto_execute;

import include.nseer_db.nseer_db_backup;
import java.io.PrintStream;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletContext;

public class GatherExpiry
{

	private nseer_db_backup db;
	private nseer_db_backup db1;
	private ResultSet rs;
	private ResultSet rs1;
	private String sql1;
	private String date;
	private String idgroup[];
	private String namegroup[];
	private String alarmgroup[];

	public GatherExpiry()
	{
		rs = null;
		rs1 = null;
		sql1 = "";
		date = "";
		idgroup = new String[0x186a0];
		namegroup = new String[0x186a0];
		alarmgroup = new String[0x186a0];
	}

	public void back(ServletContext dbApplication)
	{
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		try
		{
			nseer_db_backup dbd = new nseer_db_backup(dbApplication);
			if (dbd.conn("mysql"))
			{
				String sqldb = "show databases";
				for (ResultSet rsdb = dbd.executeQuery(sqldb); rsdb.next();)
					if (rsdb.getString("database").equals("ondemand1"))
					{
						db = new nseer_db_backup(dbApplication);
						db1 = new nseer_db_backup(dbApplication);
						if (db.conn(rsdb.getString("database")) && db1.conn(rsdb.getString("database")))
						{
							String sql3 = "delete from crm_alarm_gather_expiry";
							db.executeUpdate(sql3);
							String sql = "select * from crm_file where check_tag='1'";
							rs = db.executeQuery(sql);
							int i;
							for (i = 0; rs.next(); i++)
							{
								idgroup[i] = rs.getString("customer_ID");
								namegroup[i] = rs.getString("customer_name");
								alarmgroup[i] = rs.getString("gather_period_limit");
							}

							for (int j = 0; j < i; j++)
							{
								Date date1 = new Date();
								long Time = now.getTime() / 1000L - (long)(0x15180 * Integer.parseInt(alarmgroup[j]));
								date1.setTime(Time * 1000L);
								String time = formatter.format(date1);
								sql1 = (new StringBuilder("select * from crm_order where customer_ID='")).append(idgroup[j]).append("' and check_time<='").append(time).append("' and check_tag='1' and gather_tag!='3' order by check_time").toString();
								String sql2;
								for (rs1 = db1.executeQuery(sql1); rs1.next(); db.executeUpdate(sql2))
								{
									Date date2 = formatter.parse(rs1.getString("check_time"));
									Date now1 = new Date();
									long days1 = (long)((double)((now1.getTime() - date2.getTime()) / 0x5265c00L) + 0.5D);
									long days = (long)((double)((date1.getTime() - date2.getTime()) / 0x5265c00L) + 0.5D);
									sql2 = (new StringBuilder("insert into crm_alarm_gather_expiry(customer_ID,customer_name,order_ID,order_check_time,gather_period_limit,gather_period_expiry,period_expiry_over) values('")).append(idgroup[j]).append("','").append(namegroup[j]).append("','").append(rs1.getString("order_ID")).append("','").append(rs1.getString("check_time")).append("','").append(alarmgroup[j]).append("','").append(days1).append("','").append(days).append("')").toString();
								}

							}

							db.close();
							db1.close();
						} else
						{
							System.out.println("i am sorry!");
						}
					}

				dbd.close();
			} else
			{
				System.out.println("i am sorry!");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
